<?php
session_start();

if (!isset($_SESSION['AdminId']) || $_SESSION['AdminId'] == "") {
    echo "<script type='text/javascript'>location.href='admin.php'</script>";
}

require_once 'common/config.php';
require_once 'common/conn.php';
require_once 'common/function.php';

require_once 'db/item.db.php';
require_once 'db/itemType.db.php';

$id = isset($_GET['id']) ? $_GET['id'] : "";

$dbc = new DBC();

if ($id == "") {
    $itemName = "";
    $itemNo = "";
    $price = "";
    $imageUrl = "";
    $type = 0;
} else {
    $itemDb = new ItemDb($dbc);

    $data = $itemDb->getItemById($id);

//    $itemName = mb_convert_encoding($data[0]['ItemName'], "UTF-8", "GBK");
    $itemName = $data[0]['ItemName'];
    $itemNo = $data[0]['ItemNo'];
    $price = $data[0]['Price'];
    $imageUrl = $data[0]['ImageUrl'];
    $type = $data[0]['Type'];
}
$itemTypeDb = new ItemTypeDb($dbc);

$itemTypeArray = $itemTypeDb->getItemType();

$dbc->close();
?>
<!DOCTYPE html>
<html>
    <head>
        <title>商品修改</title>
        <meta charset="UTF-8">
        <link href="<?php echo $flatUi; ?>dist/css/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">        
    </head>
    <body>

        <div class="container">  
            <div style="text-align: center">
                <h1>                    
                    商品编辑
                </h1>
            </div>
            <form class="form-horizontal" role="form" action="action/item.action.php" method="POST" enctype="multipart/form-data">                
                <div class="form-group">
                    <label class="col-sm-2 control-label">编码</label>
                    <div class="col-sm-10">
                        <input name="idx" type="hidden" value="<?php echo $id; ?>">
                        <input class="form-control" id="itemNo" name="itemNo" type="text" value="<?php echo $itemNo; ?>" required>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">名称</label>
                    <div class="col-sm-10">
                        <input class="form-control" id="itemName" name="itemName" type="text" value="<?php echo $itemName; ?>" required>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">价格</label>
                    <div class="col-sm-10">
                        <input class="form-control" id="price" name="price" type="text" value="<?php echo $price; ?>" required>
                    </div>
                </div>                
                <div class="form-group">
                    <label class="col-sm-2 control-label">类型</label>
                    <div class="col-sm-10">
                        <select class="form-control select select-primary" data-toggle="select" name="itemType">
                            <?php
                            for ($i = 0; $i < count($itemTypeArray); $i++) {
                                $itemTypeId = $itemTypeArray[$i]['id_x'];
                                $itemTypeName = $itemTypeArray[$i]['item_type'];
                                ?>
                                <option <?php if ($itemTypeId == $type) { ?>selected="selected"<?php } ?> value="<?php echo $itemTypeId; ?>"><?php echo $itemTypeName; ?></option>
                                <?php
                            }
                            ?>                            
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">图片</label>
                    <div class="col-sm-10">
                        <input class="form-control" id="imageUrl" name="imageUrl" type="file">
                        小于2M&nbsp;
                        <?php
                        if ($imageUrl != "") {
                            echo "<a href='" . $data[0]['ImageUrl'] . "' target='_blank'>查看原图片</a>";
                        }
                        ?>
                    </div>
                </div>
                <div style="width:100%; text-align: center">                    
                    <?php if ($id == "") { ?>
                        <input type="hidden" name="action" value="create" />
                    <?php } else { ?>
                        <input type="hidden" name="idx" value="<?php echo $data[0]['id_x']; ?>"/>
                        <input type="hidden" name="action" value="edit" />
                    <?php } ?>
                    
                    <input type="submit" class="btn btn-primary" value="保存"/>
                </div>
            </form>
        </div> 
    </body>
</html>